package com.yjc.drygoods.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yjc.common.utils.PageUtils;
import com.yjc.drygoods.product.dao.AttrAttrgroupRelationDao;
import com.yjc.drygoods.product.entity.AttrAttrgroupRelationEntity;
import com.yjc.drygoods.product.service.AttrAttrgroupRelationService;
import com.yjc.drygoods.product.vo.AttrGroupRelationVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.common.utils.Query;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAttrAttrGroupRelation(List<AttrGroupRelationVo> attrGroupRelationVos) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrGroupRelationVos.stream().map(i -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(i.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(i.getAttrId());
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        for (AttrAttrgroupRelationEntity i : attrAttrgroupRelationEntities) {
            Integer count = this.baseMapper.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", i.getAttrId()));
            if (count > 0) {
                this.baseMapper.update(i, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", i.getAttrId()));
            } else {
                this.baseMapper.insert(i);
            }
        }
    }

}